package com.example.leetdemo.finash;

import java.util.Arrays;

public class Solution2401 {

    public int longestNiceSubarray(int[] nums) {
        int[] dp = new int[nums.length];
        dp[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            if (dp[i - 1] > 0 && (nums[i] & nums[i - 1]) == 0) {
                int j;
                for (j = i - 1; j > i - dp[i - 1] - 1; j--) {
                    if ((nums[j] & nums[i]) != 0) {
                        dp[i] = i - j;
                        break;
                    }
                }
                if (j <= i - dp[i - 1] - 1) {
                    dp[i] = dp[i - 1] + 1;
                }
            } else {
                dp[i] = 1;
            }
        }
        Arrays.sort(dp);
        return dp[nums.length - 1];
    }

    public static void main(String[] args) {
        Solution2401 solution2401 = new Solution2401();
        int[] a = new int[] {8,4096,524288,16777216,2097152,1024,4194304,32768,1048576,65536,4,536870912,1,134217728,128,256,8388608,2,8192,2048,16384,16,64,33554432,131072,512,262144,32,67108864};
        System.err.println(solution2401.longestNiceSubarray(a));
//        System.err.println(solution2401.longestNiceSubarray(new int[] {1,3,8,48,10}));

    }
}
